package com.bishe.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.mapper.FileMapper;
import com.bishe.pojo.FileInfo;
import com.bishe.pojo.Teacher;
import com.bishe.service.IFileService;
import com.bishe.service.TeacherService;
import com.bishe.util.EcoBootException;
import com.bishe.util.JobUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 文件接口实现
 * @date 2022/02/17 54:01:15
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implements IFileService {

    @Resource
    private TeacherService teacherService;

    @Override
    public List<FileInfo> getByDataId(String dataId) {

        return list(new LambdaQueryWrapper<FileInfo>()
                .eq(FileInfo::getDataId, dataId)
        );

    }

    @Override
    public void upload(MultipartFile file, String dataId) {
        if (StrUtil.isEmpty(dataId)){
            throw new EcoBootException("dataId为空！");
        }

        if (file==null){
            throw new EcoBootException("文件为空！");
        }

        //如果有文件先删除
        List<FileInfo> byDataId = getByDataId(dataId);
        if (CollUtil.isNotEmpty(byDataId)){
            byDataId.forEach(s ->{
                deleteFile(s.getId());
            });
        }

        String fileName = JobUtils.uploadFile(file);

        if (StrUtil.isEmpty(fileName)){
            throw new EcoBootException("格式错误！");
        }
        //上传成功则保存文件记录
        FileInfo fileInfo1 = new FileInfo();
        fileInfo1.setId(UUID.randomUUID().toString());
        fileInfo1.setDataId(dataId);
        fileInfo1.setCreateBy(StpUtil.getLoginId().toString());
        fileInfo1.setCreateTime(new DateTime());
        fileInfo1.setSuffix(FileUtil.getSuffix(fileName));
        fileInfo1.setName(FileUtil.getName(fileName));
        save(fileInfo1);
    }

    @Override
    public void deleteFile(String fileId) {
        FileInfo fileInfo = getByDateId(fileId);
        if (fileInfo == null){
            return;
        }
        String fileName = fileInfo.getName();

        String dirPath = JobUtils.getDirPath();
        //文件的下载路径
        String filePath = dirPath +File.separator + fileName;

        File file = new File(filePath);

        FileUtil.del(file);

        this.remove(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getDataId,fileId));
    }

    @Override
    public FileInfo getByDateId(String id) {
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id为空");
        }

        return getOne(new LambdaQueryWrapper<FileInfo>()
                .eq(FileInfo::getDataId, id), false);
    }

    @Override
    public void deleteFileById(String id) {
        FileInfo fileInfo = this.getById(id);
        if (fileInfo == null){
            return;
        }
        String fileName = fileInfo.getName();

        String dirPath = JobUtils.getDirPath();
        //文件的下载路径
        String filePath = dirPath +File.separator + fileName;

        File file = new File(filePath);

        FileUtil.del(file);

        this.remove(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getId,id));
    }

    @Override
    public List<FileInfo> getFiles(String id) {

        List<FileInfo> list = this.list(new LambdaQueryWrapper<FileInfo>()
                .eq(FileInfo::getCreateBy, id));

        if (CollUtil.isEmpty(list)){
            return new ArrayList<>(list.size());
        }

        Map<String, String> map = teacherService.list().stream().collect(Collectors.toMap(Teacher::getId, Teacher::getName));

        list.forEach(v -> {
            v.setName(v.getName().substring(v.getName().indexOf("_")+1,v.getName().lastIndexOf(".")));
            if (CollUtil.isNotEmpty(map)){
                v.setCreateByName(map.get(v.getCreateBy()));
            }
        });

        return list;
    }
}
